<style>

.image.results {
	padding: 0;
	text-align: left;
}

.image.results > li {
	display: inline-block;
	margin: 5px;
}

.thumbnail {
	width: 80px;
	height: 80px;
	background-size: cover;
}

.preview {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(10,10,10,0.7);
	z-index: 10;
	background-size: contain;
	background-position: center;
	background-repeat: no-repeat;
}

.preview img {
	width: 100%;
	height: 100%;
	opacity: 0;
}

.preview a, .preview .back {
	position: absolute;
	bottom: 0;
	box-sizing: border-box;
	padding: 10px;
	background-color: rgba(10,10,10,0.7);
	color: white;
	font-weight: bold;
}

.preview a {
	text-align: right;
	left: 80px;
	right: 0;
}

.preview .back {
	text-align: left;
	left: 0;
	width: 80px;
	cursor: default;
}

</style>

<ol class='image results'>
{% for result in response.d.results[0].Image %}
	<li>
		<div class='thumbnail' style='background-image: url({{ result.Thumbnail.MediaUrl }})' data-full-url='{{ result.MediaUrl }}' data-source-url='{{ result.SourceUrl }}' data-display-url='{{ result.DisplayUrl }}'></div>
	</li>
{% endfor %}
</ol>

<script>

var preview = null;
function removeExistingPreview() {
	if (preview) {
		preview.parentElement.removeChild(preview);
		preview = null;
	}
}

function showPreview(data) {
	preview = document.createElement("div");
	preview.className = "preview";
	preview.style.backgroundImage = "url(" + data['full-url'] + ")";
	
	var img = document.createElement("img");
	img.src = data['full-url'];
	preview.appendChild(img);
	
	var fullLink = document.createElement("a");
	fullLink.setAttribute("href", data['source-url']);
	fullLink.innerText = data['display-url'].split('/')[0];
	preview.appendChild(fullLink);
	
	var back = document.createElement("div");
	back.innerText = "Back";
	back.className = "back";
	back.addEventListener('click', function() {
		removeExistingPreview();
	});
	preview.appendChild(back);
	
	document.body.appendChild(preview);
}

var elements = document.querySelectorAll(".image.results .thumbnail");
for (var i=0; i<elements.length; i++) {
	var element = elements[i];
	element.addEventListener('click', function(e) {
		removeExistingPreview();
		showPreview(e.target.dataset);
	})
}

</script>
